const express = require("express");
const path = require("path");
const usermodel = require("../../day11/2-ajax实现留言板/models")
let app = express();

app.get("/", (req, res) => {
  // 加载index.html页面
  res.sendFile(path.join(__dirname, "./views/adduser.html"));
})

app.get("/adduser", async (req, res) => {
  // 加载index.html页面
  // res.json(req.query);
  // 接收用户名和年龄
  let { username, age } = req.query;
  try {
    let result = await usermodel.create(req.query);
    res.json({
      info: "添加成功",
      status: 1
    })
  } catch (err) {
    res.json({
      err: err,
      status: 0
    })
  }
})

app.get("/getdata", async (req, res) => {
  try {
    let result = await usermodel.find();
    res.json({
      info: result,
      status: 1
    })
  } catch (err) {
    res.json({
      err: err,
      status: 0
    })
  }
})

app.listen(8089);